﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Sim3D_practicum.Physics;

namespace Sim3D_practicum.Components
{
    class Border : Microsoft.Xna.Framework.DrawableGameComponent
    {            
        public const float length = 1156;//mm (1270-114=1156)
        public const float width = 150;//mm

        public Plain[] box = new Plain[6];
        static Texture2D art;

        const float height = 35;//mm
        /// <summary>
        /// A brown wooden border
        /// </summary>
        /// <param name="position">Vector from origin</param>
        /// <param name="directionL">direction of the length</param>
        /// <param name="directionW">direction of the width (expand width in this direction)</param>
        /// <param name="game"></param>
        public Border( Vector3 position, Vector3 directionL, Vector3 directionW, Game game )
            : base( game )
        {
            float newHeight = (directionW.X + directionW.Y - directionW.Z) < 0 ? -height : height;
            float YoffSet = (directionW.X + directionW.Y - directionW.Z) < 0 ? height : 0;
            box[5] = new Plain(new Vector3(0, newHeight, 0), Vector3.Multiply(directionL, length), Vector3.Add(position, new Vector3(0,0,0)));
            Vector3 offSet = Vector3.Multiply(directionW, width);//Vector3.Add(new Vector3(0, height * 2, 0), Vector3.Multiply(directionW, width));
            //long sides
            //box[1] = new Plain(new Vector3(0, height, 0), Vector3.Multiply(directionL, length), position.Clone());
            //System.Diagnostics.Debug.WriteLine(box[1].NormalVector.ToString());
            //box[2] = new Plain(Vector3.Multiply(directionL, length), new Vector3(0, height, 0), Vector3.Add(position, offSet));
            //System.Diagnostics.Debug.WriteLine(box[2].NormalVector.ToString());
            if (directionL.X < 0)
            {
                position.X += directionL.X * length;
                directionL.X = -directionL.X;
            }
            if (directionL.Z < 0)
            {
                position.Z += directionL.Z * length;
                directionL.Z = -directionL.Z;
            }
            if (directionW.X < 0)
            {
                position.X += directionW.X * width;
                directionW.X = -directionW.X;
            }
            if (directionW.Z < 0)
            {
                position.Z += directionW.Z * width;
                directionW.Z = -directionW.Z;
            }
            //top
            //box[0] = new Plain( Vector3.Multiply(directionL, length), Vector3.Multiply(directionW, width), position.Clone() );
            //long sides
            //box[1] = new Plain(new Vector3(0, newHeight, 0), Vector3.Multiply(directionL, length), position.Clone());
            //box[2] = new Plain( new Vector3( 0, blaat, 0 ), Vector3.Multiply( directionL, length ), Vector3.Add( position, directionW ) );
            //box[3] = new Plain(new Vector3(0, height, 0), Vector3.Multiply(directionW, width), position.Clone());
            //box[4] = new Plain(new Vector3(0, -height, 0), Vector3.Multiply(directionW, width), Vector3.Add(position, offSet));
        }
        
        protected override void LoadContent( )
        {
            if ( art == null )
                art = Game.Content.Load<Texture2D>( "Borderbrown" );
            base.LoadContent( );
        }
        public void Draw( GameTime gameTime, SpriteBatch sprite )
        {
            sprite.Begin( );
            for ( int i = 0; i < 1; i++ )
            {
                if ( box[i] != null )
                    sprite.Draw( art, box[i].drawFace( ), Color.White );
            }
            sprite.End( );
            base.Draw( gameTime );
        }
                
        
    }
}
